package com.ipad.net.presenter;

import android.util.Log;

import com.ipad.net.ApiCallback;
import com.ipad.net.ApiClient;
import com.ipad.net.BasePresenter;
import com.ipad.net.apistore.StoreApi;
import com.ipad.net.entity.DeliveryContainerEntity;
import com.ipad.net.entity.DeliveryCount;
import com.ipad.net.entity.DeliveryListEntity;
import com.ipad.net.entity.DeliveryListInfoEntity;
import com.ipad.net.view.IStoreView;

/**
 * Created by Administrator on 2018/12/5.
 */

public class StroePresenter extends BasePresenter<IStoreView> {
    StoreApi storeApi;

    public StroePresenter() {
        storeApi = ApiClient.getInstance().getjRetorfit().create(StoreApi.class);
    }

    //获取配送单列表
    public void getDeliveryList(String bill_code,
                                String end_create_time,
                                String end_receive_time,
                                String is_same,
                                String req_warehouse_id,
                                String start_create_time,
                                String start_receive_time,
                                String status,
                                String out_warehouse_id,
                                String page,
                                String rows,
                                String token,
                                boolean isLoading) {

        if (isLoading) {
            t.loading("加载中..");
        }

        addSubscribe(storeApi.getDeliveryList(bill_code, end_create_time, end_receive_time, is_same, req_warehouse_id, start_create_time, start_receive_time, status, out_warehouse_id, page, rows, token), new ApiCallback<DeliveryListEntity>() {
            @Override
            public void onSuccess(DeliveryListEntity o) {
                t.getDeliveryListSuccess(o);
            }

            @Override
            public void onFailuer(String msg) {
                t.error(msg);
            }

            @Override
            public void onFinish() {
                t.stopLoading();
            }
        });

    }
    //根据容器号获取配送单
    public void getDeliveryByContainer(String container,String token,boolean isLoading){
       if (isLoading){
           t.loading("加载中...");
       }

       addSubscribe(storeApi.getDeliveryInfoByContainer(container, token), new ApiCallback<DeliveryContainerEntity>() {
           @Override
           public void onSuccess(DeliveryContainerEntity o) {
               t.getDeliveryByContainerSuccess(o);
           }

           @Override
           public void onFailuer(String msg) {
                t.error(msg);
           }

           @Override
           public void onFinish() {
                t.stopLoading();
           }
       });
    }

    //获取配送单详情
    public void getDeliveryDetails(String id, String page, String rows, String token, boolean isLoading) {
        if (isLoading) {
            t.loading("加载中..");
        }
        addSubscribe(storeApi.getDeliveryDetails(id, page, rows, token), new ApiCallback<DeliveryListInfoEntity>() {
            @Override
            public void onSuccess(DeliveryListInfoEntity o) {
                t.getDeliveryDetailSuccess(o);
            }

            @Override
            public void onFailuer(final String msg) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.error(msg);
                    }
                });
            }

            @Override
            public void onFinish() {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.stopLoading();
                    }
                });
            }
        });

    }

    //获取配送单数量
    public void getDeliveryCount(String token, boolean isLoading) {
        if (isLoading) {
            t.loading("加载中..");
        }
        addSubscribe(storeApi.getDeliveryCount(token), new ApiCallback<DeliveryCount>() {
            @Override
            public void onSuccess(final DeliveryCount o) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.getDeliveryCountSuccess(o);
                    }
                });
            }

            @Override
            public void onFailuer(final String msg) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.error(msg);
                    }
                });
            }

            @Override
            public void onFinish() {
                post(new Runnable() {
                    @Override
                    public void run() {
                        t.stopLoading();
                    }
                });
            }
        });
    }
}
